package com.ms.oschina.viewholder;

import java.util.ArrayList;
import java.util.List;

import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ms.oschina.R;
import com.ms.oschina.base.AppContext;
import com.ms.oschina.base.BaseViewHolder;
import com.ms.oschina.bean.Tweet;
import com.ms.oschina.bean.User;
import com.ms.oschina.util.RichTextHelper;
import com.ms.oschina.util.StringUtils;
import com.ms.oschina.util.UiUtils;
import com.ms.oschina.view.CircleImageView;
import com.ms.oschina.view.TextViewFixTouchConsume;
import com.squareup.picasso.Picasso;

public class TweetViewHolder extends BaseViewHolder<Tweet> implements
		OnClickListener {
	private CircleImageView civTweetUserAvatar;
	private TextView tvTweetUserName;
	private TextView tvTweetTweetContent;
	private ImageView ivTweetImg;
	private TextView tvTweetTweetTime;
	private RelativeLayout rlTweetLike;
	private ImageView ivLike;
	private TextView rlTweetCommentCount;
	private TextView tvTweetLikeInfo;

	int skipIndex = -1;
	private List<Tweet> list;

	public TweetViewHolder(List<Tweet> list) {
		this.list = list;
		userName = AppContext.getUserName();
	}

	/**
	 * Find the Views in the layout<br />
	 * <br />
	 * Auto-created by Android Layout Finder
	 * (http://www.buzzingandroid.com/tools/android-layout-finder)
	 * 
	 * @param view
	 */
	private void findViews(View view) {
		civTweetUserAvatar = (CircleImageView) view
				.findViewById(R.id.civ_aboutme_userAvatar);
		tvTweetUserName = (TextView) view.findViewById(R.id.iv_event_title);
		tvTweetTweetContent = (TextViewFixTouchConsume) view
				.findViewById(R.id.tv_aboutme_commentContent);
		ivTweetImg = (ImageView) view.findViewById(R.id.iv_tweet_tweetImg);
		tvTweetTweetTime = (TextView) view.findViewById(R.id.iv_event_time);
		rlTweetLike = (RelativeLayout) view.findViewById(R.id.rl_tweet_like);
		ivLike = (ImageView) view.findViewById(R.id.iv_tweet_like);
		rlTweetCommentCount = (TextView) view
				.findViewById(R.id.rl_tweet_commentCount);
		tvTweetLikeInfo = (TextViewFixTouchConsume) view
				.findViewById(R.id.tv_aboutme_mySeedInfo);
	}

	@Override
	public View initView() {
		View view = UiUtils.inflate(R.layout.item_tweet_datas);
		findViews(view);
		return view;
	}

	private String userName;
	private int position;

	@Override
	public void setData(int position, Tweet data) {
		this.position = position;

		rlTweetLike.setOnClickListener(this);
		String portrait = data.getPortrait();
		if (!TextUtils.isEmpty(portrait)) {
			Picasso.with(UiUtils.getContext()).load(portrait)
					.placeholder(R.drawable.widget_dface)
					.into(civTweetUserAvatar);
		}

		tvTweetUserName.setText(data.getAuthor());
		tvTweetTweetTime.setText(StringUtils.friendly_time(data.getPubDate()));
		rlTweetCommentCount.setText(data.getCommentCount());

		String imgSmall = data.getImgSmall();
		if (TextUtils.isEmpty(imgSmall)) {
			ivTweetImg.setVisibility(View.GONE);
		} else {
			ivTweetImg.setVisibility(View.VISIBLE);
			Picasso.with(UiUtils.getContext()).load(imgSmall)
					.placeholder(R.drawable.img_load).into(ivTweetImg);
		}
		// 设置动弹内容
		RichTextHelper.setTextRecombinationContent(tvTweetTweetContent,
				data.getBody());
		// 设置底部点赞
		int isLiked = data.getIsLike();
		isLike = isLiked == 1 ? true : false;
		ivLike.setImageResource(isLike ? R.drawable.ic_likeed
				:R.drawable.ic_unlike_small );

		int green = UiUtils.getContext().getResources().getColor(R.color.green);
		RichTextHelper.showLike(tvTweetLikeInfo, 5, data, green);
	}

	private boolean isLike;
	private User user;

	@Override
	public void onClick(View v) {
		boolean login = AppContext.isLogin();
		if (!login) {
			UiUtils.showToast("请先登录");
			return;
		}


		Tweet tweet = list.get(position);
		ArrayList<User> likeUser = (ArrayList<User>) tweet.getLikeUser();

		isLike = !isLike;
		if (isLike) {
			RichTextHelper.likeAni(ivLike);
			addLike(tweet, likeUser);
		} else {
			tweet.setIsLike(0);
			likeUser.remove(user);
		}

		tweet.setLikeCount(isLike? tweet.getLikeCount()+1: tweet.getLikeCount()-1);
		if (tweet.getLikeCount() > 0) {
			tvTweetLikeInfo.setVisibility(View.VISIBLE);
		} else {
			tvTweetLikeInfo.setVisibility(View.GONE);
		}
		// 局部刷新
		setData(position, tweet);
	}

	public void addLike(Tweet tweet, ArrayList<User> likeUser) {
		user = new User();
		user.setId(Integer.parseInt(AppContext.getUid()));
		user.setName(userName);
		likeUser.add(0, user);
		
		tweet.setIsLike(1);
		setData(position, tweet);
	}
}
